<template>
	<view class="huicontainer">

		<view class="uni-padding-wrap uni-common-mt">
			<!-- #ifdef MP-WEIXIN --> 
			<view class="loginbtn">
				<view style="text-align: center; margin-bottom: 5px; font-weight: bold;font-size: 16px;">第一步</view>
				<button type="primary" :disabled="!isgo" open-type="getUserInfo" @click="wxLogin">获取微信信息</button>
				<view style="text-align: center; margin-top: 15px; font-weight: bold;font-size: 16px;">第二步</view>
				<button type="primary" class="mt-20" :disabled="isgo" open-type="getPhoneNumber"
					@getphonenumber="phonelogin">手机号登录</button>
			</view>
			<!-- #endif --> 
		</view>
		<foot />
	</view>

</template>
<script>
	var app = getApp();
	export default {
		data() {
			return {
				phone:'',
				smscode:'',
				code: '',
				isgo: true,
				model: {},
				openid: undefined
			}
		},
		onLoad() {},
		methods: {
			sendSMS(){
				
				if (!this.phone || this.phone.length !== 11) {
					uni.showToast({
						title: '请输入正确的手机号'
					})
					return;
				}
				this.$request.post(this.$api.common.sendsms,{phone:this.phone,token:this.smsCode},'form').then(res => {
					if (res) {
						this.smsCode=res.data
						uni.showToast({
							title: '发送成功'
						})
					}
				});
			},
			smslogin(){
				
				if (!this.phone || this.phone.length !== 11) {
					uni.showToast({
						title: '请输入正确的手机号'
					})
					return;
				}
				if (!this.code || this.code=='') {
					uni.showToast({
						title: '请输入验证码'
					})
					return;
				}
				var pinf = plus.push.getClientInfo();
				var cid = pinf.clientid;//客户端标识  
				this.$request.post(this.$api.user.userlogin,{phone:this.phone,vrcode:this.code,token:this.smsCode,clientid:cid}).then(res => {
					if (res) {
						this.$storage.setData('token', res.token);
						this.$storage.setJson('user', res.data);
						this.$storage.setData('clientid', cid);
						uni.navigateBack({
							delta: 1,
							success: function(e) {
								// var page = getCurrentPages().pop();
								// if (page == undefined || page == null) return;
								// page.onLoad();
							}
						})
					}
				});
			},
			phonelogin: function(e) {
				console.log('model',this.model, e)
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					this.$request.post(this.$api.owner.phonelogin, {
						encrypted: e.detail.encryptedData,
						iv: e.detail.iv,
						name: this.model.name,
						picture: this.model.picture,
						province: this.model.province,
						city: this.model.city,
						district: this.model.district,
						sex: this.model.sex,
						openid: this.openid,
						unionid: this.model.unionid
					}).then(res => {
						console.log('res')
						this.$storage.setData('token', res.token);
						this.$storage.setJson('user', res.data);
						this.$storage.setData('online', '1');
						uni.navigateBack({
							delta: 1,
							success: function(e) {
								var page = getCurrentPages().pop();
								if (page == undefined || page == null) return;
								page.onLoad();
							}
						})
						// if(!res.mopenid || res.mopenid==''){
						// 	uni.navigateTo({
						// 	    url: '../login/mp'
						// 	});
						// }
						// else{
						// 	uni.navigateBack({
						// 		delta: 1,
						// 		success: function(e) {
						// 			var page = getCurrentPages().pop();
						// 			if (page == undefined || page == null) return;
						// 			page.onLoad();
						// 		}
						// 	})
						// }
						
					});
				} else {
					this.$request.post(this.$api.owner.phonelogin, {
						encrypted: e.detail.encryptedData,
						iv: e.detail.iv,
						name: this.model.name,
						picture: this.model.picture,
						province: this.model.province,
						city: this.model.city,
						district: this.model.district,
						sex: this.model.sex,
						openid: this.openid,
						unionid: this.model.unionid
					}).then(res => {
						console.log('res')
						this.$storage.setData('token', res.token);
						this.$storage.setJson('user', res.data);
						this.$storage.setData('online', '1');
						uni.navigateBack({
							delta: 1,
							success: function(e) {
								var page = getCurrentPages().pop();
								if (page == undefined || page == null) return;
								page.onLoad();
							}
						})
					});
				}
				
			},
			getopen: function(e) {
				var that = this
				uni.login({
					provider: 'weixin',
					success: function(res) {
						uni.getUserInfo({
							provider: 'weixin',
							success: infoRes => {
								that.model = infoRes.userInfo
								console.log('login',infoRes)
								that.$request.post(that.$api.user.getopen, {
									code: res.code
								}, 'form').then(res => {
									that.openid = res.data
									that.isgo = false
								})
							}
						});

					}
				});
			},
			wxGetUserProfile: function() {
				return new Promise((resolve, reject) => {
					uni.getUserProfile({
						lang: 'zh_CN',
						desc: '用于完善会员资料',
						success: (res) => {
							resolve(res)
						},
						// 失败回调
						fail: (err) => {
							reject(err)
						}
					})
				})
			},

			wxSilentLogin: function() {
				return new Promise((resolve, reject) => {
					uni.login({
						success(res) {
							resolve(res.code)
						},
						fail(err) {
							reject(err)
						}
					})
				})
			},
			wxLogin: function(e) {
				console.log('e',e)
				var that = this
				let p1 = this.wxSilentLogin()
				let p2 = this.wxGetUserProfile()
				Promise.all([p1, p2]).then((res) => {
					console.log('res',res)
					let code = res[0]
					let iv = res[1].iv
					let encryptedData = res[1].encryptedData
					// 请求服务器
					that.$request.post(that.$api.owner.getopen, {
						code: code,
						encrypted: res[1].encryptedData,
						iv: res[1].iv,
						rawData: res[1].rawData,
						signature: res[1].signature
					}).then(res => {
						console.log(res)
						that.model = res.data
						that.openid = res.data.openid
						that.isgo = false
					})
				}).catch((err) => {
					console.log(err)
				})
			}
		}
	}
</script>

<style>
	.logo {
		margin-top: 100rpx;
		text-align: center;
	}

	.logo image {
		width: 300rpx;
		height: 300rpx;
	}

	.loginbtn {
		width: 80%;
		margin: 200rpx auto 0 auto;
	}
</style>
